package com.foxzzz.dynamic.programming;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Solution300 {
    public int lengthOfLIS1(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }

        int dp[] = new int[nums.length];
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            //max{dp[i-1]...dp[0]}+1;
            int numI = 1;
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    numI = Math.max(numI, 1 + dp[j]);
                }
            }
            dp[i] = numI;
            max = Math.max(max, numI);
        }

        return max;
    }

    public int lengthOfLIS(int[] nums) {
        int dp[] = new int[nums.length];
        int len = 0;
        for (int i = 0; i < nums.length; i++) {
            int index = Arrays.binarySearch(dp, 0, len, nums[i]);
            if (index < 0) {
                index = -index-1;
            }
            dp[index] = nums[i];
            if (index == len) {
                len++;
            }
        }
        return len;
    }


    public static void main(String[] args) {
        System.out.println(new Solution300().lengthOfLIS(new int[]{10, 9, 2, 5, 3, 7, 101, 18}));
        System.out.println(new Solution300().lengthOfLIS(new int[]{4, 10, 4, 3, 8, 9}));
        System.out.println(new Solution300().lengthOfLIS(new int[]{1, 3, 6, 7, 9, 4, 10, 5, 6}));
    }
}
